package edu.cuit.avatar.sys;

import java.time.LocalDate;

/**
 * 学员选课系统-学生类
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/5 9:59
 */
public class Student {
    private static int nextId;
    /**
     * 标识id
     */
    private int id;
    /**
     * 组合关系(整体与部分:被聚集对象Name对聚集对象Student所独有)
     */
    private Name name;

    /**
     * 学生专业
     */
    private String major;

    /**
     * 学生性别
     */
    private char gender = 'M';

    /**
     * 入学日期
     */
    private LocalDate entryDate;

    /**
     * 聚集关系(整体与部分:被聚集对象Address被多个聚集对象Student共享)
     */
    private Address address;

    /**
     * 学员所选择的课程
     * 关联关系 (多对多)
     */
    private Course[] courses;
    /**
     * 学生选择的课程数
     */
    private int size;

    public Student(){
        this.id = nextId++;
        courses = new Course[10];
    }

    public Student(String firstName, String lastName) {
        this();
        this.name = new Name(firstName, lastName);
    }

    public int getId() {
        return id;
    }


    public Name getName() {
        return name;
    }

    public void setName(Name name) {
        this.name = name;
    }

    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public LocalDate getEntryDate() {
        return entryDate;
    }

    public void setEntryDate(LocalDate entryDate) {
        this.entryDate = entryDate;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public static int getNextId() {
        return nextId;
    }

    public int getSize() {
        return size;
    }

    /**
     * 学生选择具体的课程(关联): 将所选择的课程添加进courses数组
     * @param course 所选择的课程
     */
    public void take(Course course){
        //如果数组元素已满，扩充数组
        if(size == courses.length){
            //创建一个更大的临时数组
            var temp = new Course[courses.length * 2];
            //将原数组中的元素拷贝进临时数组
            System.arraycopy(courses, 0, temp, 0, courses.length);
            //改变原数组引用为更大的临时数组的引用
            courses = temp;
        }
        courses[size++] = course;
    }
}
